package com.han.dao;

import com.han.pojo.Department;
import com.han.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: zh
 * @date: 2021/10/26 22:27
 * @version: 1.0
 */
public class EmployeeDao {

    public static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartmentDao departmentDao;

    static {
        employees = new HashMap<Integer, Employee>();
        employees.put(101, new Employee(101, "AA","zh@qq.com",0,new Department(101, "教学部")));
        employees.put(102, new Employee(102, "BB","zh@qq.com",1,new Department(102, "市场部")));
        employees.put(103, new Employee(103, "CC","zh@qq.com",0,new Department(103, "教研部")));
        employees.put(104, new Employee(104, "DD","zh@qq.com",1,new Department(104, "运营部")));
        employees.put(105, new Employee(105, "EE","zh@qq.com",0,new Department(105, "后勤部")));
    }

    /**
     * 查询所有员工
     * @return
     */
    public Collection<Employee> getAllEmployees(){
        return employees.values();
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }

    /**
     * 主键自增
     */
    private static Integer initId = 106;

    /**
     * 增加员工
     * @param employee
     */
    public void addEmployee(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }

        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));

        employees.put(employee.getId(),employee);
    }

    /**
     * 删除员工
     * @param id
     */
    public void deleteEmployee(Integer id){
        employees.remove(id);
    }
}
